package com.xckj.teacher.ui.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.teacher.R;
import com.xckj.teacher.bean.TextbookBean;

import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 年级
 * @Date: 2024/6/11 22:36
 */
public class GradeAdapter extends RecyclerView.Adapter {
    private List<TextbookBean> textbookBeans;

    private OnItemClickListener onItemClickListener;

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<TextbookBean> textbookBeans) {
        this.textbookBeans = textbookBeans;
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grade, parent, false));
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        TextbookBean textbookBean = textbookBeans.get(position);
        itemViewHolder.itemView.setOnClickListener(view -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(view, position);
                if (position != RecyclerView.NO_POSITION) {
                    for (TextbookBean bean:textbookBeans) {
                        bean.isClickState = false;
                    }
                    textbookBeans.get(position).isClickState = true;
                    notifyDataSetChanged();
                }
            }
        });
        itemViewHolder.tvGrade.setText(textbookBean.gradeName);
        if (textbookBean.isClickState) {
            itemViewHolder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
            itemViewHolder.tvGrade.setTextColor(Color.parseColor("#2979FF"));
        }else {
            itemViewHolder.itemView.setBackgroundColor(Color.parseColor("#F8F8F8"));
            itemViewHolder.tvGrade.setTextColor(Color.parseColor("#333333"));
        }
    }

    @Override
    public int getItemCount() {
        return textbookBeans == null ? 0 : textbookBeans.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvGrade;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tvGrade = itemView.findViewById(R.id.tv_grade);
        }
    }

    public interface OnItemClickListener {
        /**
         * Item的点击事件
         *
         * @param view     当前视图
         * @param position 当前索引
         */
        void onItemClick(View view, int position);
    }
}
